package stark.app.function.game;

import android.util.Log;

import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.lxj.xpopup.interfaces.OnConfirmListener;
import com.stark.game.jfcz.JfczLevelData;
import com.stark.game.jfcz.JfczLevelDataProvider;
import com.stark.game.jfcz.callback.JfczInitListener;
import com.stark.game.jfcz.callback.OnGameFinished;
import com.stark.game.jfcz.callback.OnPointBiuFinished;

import stark.app.R;
import stark.app.databinding.ActivityJfczDemoBinding;
import stark.common.basic.base.BaseNoModelActivity;
import stark.common.basic.utils.DialogUtil;

/**
 * Author：daizhongbin on 2021/8/18
 * Email：1027154338@qq.com
 * Desc: 游戏demo功能界面
 */
public class JfczDemoActivity extends BaseNoModelActivity<ActivityJfczDemoBinding> {

    //关卡数据提供器
    private JfczLevelDataProvider mLevelDataProvider;
    private int curLevel = 1;
    private SPUtils mSpUtils = SPUtils.getInstance("jfcz");
    private static final String KEY_LEVEL = "key_level";

    @Override
    protected int onCreate() {
        return R.layout.activity_jfcz_demo;
    }

    @Override
    protected void initView() {
        curLevel = mSpUtils.getInt(KEY_LEVEL, 1);

        //初始化监听器
        mDataBinding.jfczView.setInitListener(new JfczInitListener() {
            @Override
            public void onInit(int maxPointCount) {
                if (mLevelDataProvider == null) {
                    mLevelDataProvider = new JfczLevelDataProvider(maxPointCount);
                }
                mLevelDataProvider.setMaxPointCount(maxPointCount);

                updateLevelData(curLevel);
            }
        });
        //本关游戏结束监听器
        mDataBinding.jfczView.setOnGameFinishedListener(new OnGameFinished() {
            @Override
            public void onSuccess() {
                showCurLevelRetDialog(true);
            }

            @Override
            public void onFail() {
                showCurLevelRetDialog(false);
            }
        });
        //正在发射的小球发射完毕监听器
        mDataBinding.jfczView.setOnPointBiuFinishedListener(new OnPointBiuFinished() {
            @Override
            public void onPointFinished(int index) {
                Log.i("dzb", "onPointFinished: index = " + index);
            }
        });
        mDataBinding.btnPause.setOnClickListener(v -> {
            mDataBinding.jfczView.pause();
        });
        mDataBinding.btnResume.setOnClickListener(v -> {
            mDataBinding.jfczView.resume();
        });
        mDataBinding.btnRestart.setOnClickListener(v -> {
            mDataBinding.jfczView.restart();
        });
        mDataBinding.btnPreLevel.setOnClickListener(v -> {
            clickPreLevel();
        });
        mDataBinding.btnNextLevel.setOnClickListener(v -> {
            clickNextLevel();
        });
    }

    @Override
    protected void initData() {
    }

    private void saveLevel(int level) {
        mSpUtils.put(KEY_LEVEL, level);
    }

    private boolean updateLevelData(int level) {
        JfczLevelData levelData = mLevelDataProvider.getLevelData(level);
        if (levelData == null) {
            ToastUtils.showLong("没有更多关卡数据了");
            return false;
        }
        mDataBinding.jfczView.setCount(levelData.initCount, levelData.restCount);
        mDataBinding.jfczView.setRotateSpeed(levelData.rotateSpeed);
        mDataBinding.jfczView.setBiuSpeed(levelData.biuSpeed);

        //更新关卡标题
        mDataBinding.tvLevel.setText("第 " + level + " 关");

        //保存关卡
        saveLevel(level);
        return true;
    }

    private void clickPreLevel() {
        if (curLevel > 1) {
            curLevel--;
            updateLevelData(curLevel);
        } else {
            ToastUtils.showLong("没有上一关了");
        }
    }

    private void clickNextLevel() {
        curLevel++;
        boolean ret = updateLevelData(curLevel);
        if (!ret) {
            curLevel--;
        }
    }

    private void showCurLevelRetDialog(boolean success) {
        String content = success ? "恭喜您完成本关" : "很遗憾您失败了";
        String confirm = success ? "下一关" : "重新挑战";
        DialogUtil.asConfirmNotCancel(this, "", content, confirm, new OnConfirmListener() {
            @Override
            public void onConfirm() {
                if (success) {
                    clickNextLevel();
                } else {
                    mDataBinding.jfczView.restart();
                }
            }
        });
    }

}
